﻿using lcwb.Shared.Model;
using System.Net.Http;
using System.Net.Http.Json;
using lcwb.Shared.Api;
namespace lcwb.Shared.Service
{
    public class CourseService
    {
        private readonly HttpClient _httpClient;
        public CourseService(IHttpClientFactory httpClientFactory)
        {
            _httpClient = httpClientFactory.CreateClient("ServerApi");
        }

        public async Task<List<Course>> GetCoursesAsync()
        {
            try
            {
                var response = await _httpClient.GetAsync("/api/CourseAdmin/Courses/Get");
                if (response.IsSuccessStatusCode)
                {

                    ApiResponse<List<Course>>? res = await response.Content.ReadFromJsonAsync<ApiResponse<List<Course>>>();
                    if (res != null)
                    {
                        return res.Data ?? new List<Course>();
                    }
                    else
                    {
                        return new List<Course>();
                    }
                }
                else
                {
                    Console.Error.WriteLine($"API request failed with status code: {response.StatusCode}");
                    return new List<Course>();
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"An error occurred while fetching courses: {ex.Message}");
                return new List<Course>();
            }
        }
        public async Task<Course> GetCourseByIdAsync(int Id)
        {
            try
            {
                string Api = $"/api/CourseAdmin/Course/Get/{Id}";
                var response = await _httpClient.GetAsync(Api);
                if (response.IsSuccessStatusCode)
                {
                    ApiResponse<Course>? res = await response.Content.ReadFromJsonAsync<ApiResponse<Course>>();
                    if (res != null)
                    {
                        return res.Data ?? new Course();
                    }
                    else
                    {
                        return new Course();
                    }
                }
                else
                {
                    Console.Error.WriteLine($"API request failed with status code: {response.StatusCode}");
                    return new Course();
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"An error occurred while fetching courses: {ex.Message}");
                return new Course();
            }
        }
        public async Task<List<Chapter>> GetChapters(int Id)
        {
            try
            {
                string Api = $"api/CourseAdmin/Course/{Id}/Chapters";
                var response = await _httpClient.GetAsync(Api);
                if (response.IsSuccessStatusCode)
                {
                    ApiResponse<List<Chapter>>? res = await response.Content.ReadFromJsonAsync<ApiResponse<List<Chapter>>>();
                    if (res != null)
                    {
                        return res.Data ?? new List<Chapter>();
                    }
                    else
                    {
                        return new List<Chapter>();
                    }
                }
                else
                {
                    Console.Error.WriteLine($"API request failed with status code: {response.StatusCode}");
                    return new List<Chapter>();
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"An error occurred while fetching courses: {ex.Message}");
                return new List<Chapter>();
            }
        }
        public async Task<Chapter> GetChapterById(int ChapterId)
        {
            try
            {
                string Api = $"/api/CourseAdmin/Chapter/Get/{ChapterId}";
                var response = await _httpClient.GetAsync(Api);
                if (response.IsSuccessStatusCode)
                {
                    return await response.Content.ReadFromJsonAsync<Chapter>() ?? new Chapter();
                }
                else
                {
                    Console.Error.WriteLine($"API request failed with status code: {response.StatusCode}");
                    return new Chapter();
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"An error occurred while fetching courses: {ex.Message}");
                return new Chapter();
            }
        }
    }
}
